var app = getApp();
Page(Object.assign({},{
    data:{
        datalist : [],
        inited : false,
        goodslist : [{},{},{}],
        shoplist : [],
        isshowredbag : false,
        isshowp : false,
        posterurl : false,
        posterurl : false,
        isshownum : false,
        buynum : 1,
		siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
    },
    onLoad:function(options){
        var self = this;
        
        var zfid = options.zfid ? options.zfid : 0;
        var cid = options.id ? options.id : 0;
        if( options.scene ){
            var list = options.scene.split('_');
            cid = list[0] ? list[0] : 0;
            zfid = list[1] ? list[1] : 0;
        }

        this.data.zfid = zfid;
        
        this.data.options = options;
        app.com.auth(function(user){
            app.com.http('card','POST',{op:'info',id:cid,zfid:zfid},0,false,'','',function(res){
                if (!res.data.errno) {
                    self.setData({
                        params : res.data.data.params,
                        userinfo : res.data.data.userinfo,
                        card : res.data.data.card,
                        shop : res.data.data.shop,
                        sharedata : res.data.data.sharedata,
                        allgoods : res.data.data.allgoods ? res.data.data.allgoods : [],
                        shoplist : res.data.data.shoplist ? res.data.data.shoplist : [],
                        qrcode : res.data.data.card.takedid > 0 ? app.com.murl('img',{op:'hexiaocard',cid:res.data.data.card.takedid,m:'zofui_sales'}) : '',
                    },function(){
                        self.setData({
                            inited : true,
                            isshowredbag : self.data.card.issharemoney == 1,
                        });
                    });

                    wx.setNavigationBarTitle({
                        title:  res.data.data.card.name,
                    });

                    // 关闭下拉刷新
                    wx.stopPullDownRefresh();
                }else{
                    app.com.alert(res.data.message,function(){
                        app.com.backPage(options.zfid);
                    });
                }
            });
        });
    },
    toggleDetail(){
        this.setData({
            showDetail : !this.data.showDetail,
        })
    },
    onPullDownRefresh: function () {
        if( this.data.isdown ) return false;
        this.setData({
            isdown : true,
        });
        
        this.onLoad( this.data.options );
        this.setData({
            isdown : false,
        });
    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: self.data.sharedata.title,
            path: self.data.sharedata.path,
            imageUrl : self.data.sharedata.img,
        }
    },
    takecard(e,type){
        var self = this;
        if( !type && self.data.card.ispay == 1 ){
            self.setData({
                isshownum : true
            })
            return false;
        }
        var postdata = {
            op : 'takecard',
            id : self.data.card.id,
            fxuid : self.data.zfid,
            buynum : self.data.buynum,
        };
        app.com.http('card','POST',postdata,0,true,'','',function(res){
            
            if (!res.data.errno && res.data) {
                app.com.toast( res.data.message );
                self.setData({
                   ['card.istaked'] : 1,
                   ['card.takedid'] : res.data.data.takedid,
                });
                return false;
            }else if(res.data.errno == 3){
                wx.requestPayment({
					'appId' : res.data.data.appId,
                    'timeStamp': res.data.data.timeStamp,
                    'nonceStr': res.data.data.nonceStr,
                    'package': res.data.data.package,
                    'signType': 'MD5',
                    'paySign': res.data.data.paySign,
                    'success': function (payres) {
                        if( payres.errMsg == 'requestPayment:ok' ){
                            app.com.alert('已支付完成',function(){
                                
                                wx.navigateTo({
                                    url : '/zofui_sales/card/mylist'
                                });
                                
                            });
                            return false;
                        }
                    },
                    'fail': function (payres) {
                        
                    }
                });
            }else if( res.data.errno == 2 ){

                app.com.confirm(res.data.message,function(){
                    var tourl = '/zofui_sales/ucard/buycard';
                    wx.navigateTo({
                      url: tourl
                    });
                },false,'取消','购买会员');

            }else{
                app.com.toast( res.data.message );
            }
        });
        self.subMess();
    },
    shareit(e){
        this.setData({
            isshowp : !this.data.isshowp
        })
    },
    getPoster(){
        var self = this;
        var postdata = {
            op : 'card',
            path : self.data.sharedata.path,
            id: self.data.card.id,
        };
        app.com.http('poster','POST',postdata,0,true,'','',function(res){
            if (!res.data.errno && res.data ) {
                self.setData({
                    posterurl : res.data.data.url
                })
            }else{
                app.com.alert(res.data.message);
            }
        });
    },
    saveposter(){
        var self = this;
        wx.downloadFile({  
            url: self.data.posterurl,
            success: function (res) {   
                wx.saveImageToPhotosAlbum({
                filePath: res.tempFilePath,  
                success(res) {  
                    app.com.toast('已保存到本地相册');  
                },  
                fail(res) {  
                    app.com.toast('保存失败'); 
                }  
              })  
            },  
            fail: function (res) {  
                app.com.toast('下载文件失败'); 
            }  
        })
    },
    changebuynum(e){
        this.setData({
            buynum : e.detail
        })
    },
    buyit(){
        var e = '';
        this.takecard(e,'buy');
    },
    chooseCard(e){
        this.setData({
            t : e.detail.value
        })
    },
    hideredbag(){
        this.setData({
            isshowredbag : !this.data.isshowredbag
        })
    },
    isshownumfn(){
        this.setData({
            isshownum : !this.data.isshownum
        })
    },
    name(e){
        this.setData({
            name : e.detail.value
        })
    },
    cardadclick(e){
        if(this.data.card.adtype == 0){
            this.navigateto(e);
        }else if(this.data.card.adtype == 2){
            app.com.toweburl(e);
        }
    },
    tel(e){
        this.setData({
            tel : e.detail.value
        })
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    showimages : function( e ){
        app.com.showimages( e );
    },
    FormIdsubmit : function(e){
        if( app.globalData.formarr.length <= 20 ) app.globalData.formarr.push(e.detail.formId);
    },
    subMess(){
        var self = this;
        if( wx.requestSubscribeMessage ){
            wx.requestSubscribeMessage({
                tmplIds : self.data.params.submess,
                complete : function(){}
            }) 
        }       
    }
}))